﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Company.Shell.Enums;
using Company.Shell.Interfaces;
using LiveCharts.Wpf;
using System.Windows;
using System.Windows.Input;

namespace Company.Shell.ViewModels
{
    internal class ProgramViewModel : ObservableObject
    {
        private CartesianChart? chart;
        public ICommand? MouseWorkCommand { get; }
        public ICommand? ChartClickCommand { get; }
        public ICommand? ChartLoadedCommand { get; }
        public ICommand? DecreaseXAxisCommand { get; }
        public ICommand? IncreaseXAxisCommand { get; }
        public IMouseManager MouseManager { get; }
        public IProgramManager ProgramManager { get;}
        public ProgramViewModel(
            IMouseManager mouseManager,
            IProgramManager programManager )
        {
            MouseManager = mouseManager;
            ProgramManager = programManager;
            MouseWorkCommand = new RelayCommand<MouseWorkMode>(OnMouseWorkCommand);
            ChartLoadedCommand = new RelayCommand<RoutedEventArgs>(OnChartLoadedCommand);
            ChartClickCommand = new RelayCommand<MouseButtonEventArgs>(OnChartClickCommand);
            DecreaseXAxisCommand = new RelayCommand(() => ProgramManager.Program.DecreaseXAxis());
            IncreaseXAxisCommand = new RelayCommand(() => ProgramManager.Program.IncreaseXAxis());
        }

        private void OnChartClickCommand(MouseButtonEventArgs? args)
        {
            if (chart == null) return;
            var position = args.GetPosition(chart);
            double x = GetXFromPositon(position.X, chart);
            double y = GetYFromPositon(position.Y, chart);
            ProgramManager.Add(new LiveCharts.Defaults.ObservablePoint() { X = x, Y = y }, MouseManager.WorkMode);

        }

        private double GetYFromPositon(double y, CartesianChart chart)
        {
            var yAxis = chart.AxisY.FirstOrDefault();
            if (yAxis == null) return 0;
            double chartWidth = chart.ActualHeight;
            double min = yAxis.MinValue;
            double max = yAxis.MaxValue;
            return max - (max - min) * (y / chartWidth);
        }

        private double GetXFromPositon(double x, CartesianChart chart)
        {
            var xAxis = chart.AxisX.FirstOrDefault();
            if (xAxis == null) return 0;
            double chartWidth = chart.ActualWidth;
            double min = xAxis.MinValue;
            double max = xAxis.MaxValue;
            return min + (max - min) * (x / chartWidth);
        }

        private void OnChartLoadedCommand(RoutedEventArgs? args)
        {
            if (chart == null) 
            {
                chart = args?.OriginalSource as CartesianChart;
            }
        }

        private void OnMouseWorkCommand(MouseWorkMode mode)
        {
            switch (mode)
            {
                case MouseWorkMode.默认操作:
                    ProgramManager.ShowTemperature = System.Windows.Visibility.Visible;
                    ProgramManager.ShowHumidity = System.Windows.Visibility.Visible;
                    break;
                case MouseWorkMode.编制温度:
                    ProgramManager.ShowTemperature = System.Windows.Visibility.Visible;
                    ProgramManager.ShowHumidity = System.Windows.Visibility.Collapsed;
                    break;
                case MouseWorkMode.编制湿度:
                    ProgramManager.ShowTemperature = System.Windows.Visibility.Collapsed;
                    ProgramManager.ShowHumidity = System.Windows.Visibility.Visible;
                    break;
                default:
                    break;
            }
        }
    }
}
